package com.tools_s7.net;


import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

public class APIManager {

    private volatile static APIManager sInstance;
    private Retrofit retrofit;
    // 超时时间
    private int DEFAULT_TIME_OUT = 30, DEFAULT_READ_TIME_OUT = 30;

    private APIManager() {
        // 创建 OKHttpClient
        OkHttpClient.Builder httpBuilder = new OkHttpClient.Builder();
        // 设置超时
        httpBuilder.connectTimeout(DEFAULT_TIME_OUT, TimeUnit.SECONDS) //连接超时时间
                .writeTimeout(DEFAULT_READ_TIME_OUT, TimeUnit.SECONDS)  //写操作 超时时间
                .readTimeout(DEFAULT_READ_TIME_OUT, TimeUnit.SECONDS); //读操作超时时间

      /*  // 添加公共参数拦截器（如下）
        HttpCommonInterceptor commonInterceptor = new HttpCommonInterceptor.Builder()
                .addHeaderParams("paltform", "android")
                .addHeaderParams("userToken", "1234343434dfdfd3434")
                .addHeaderParams("userId", "123445")
                .build();
        httpBuilder.addInterceptor(commonInterceptor);*/

        // 创建Retrofit
        retrofit = new Retrofit.Builder()
                .baseUrl("https://www.tianqiapi.com/")
                .client(httpBuilder.build())
                .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
    }

    // 获取 APIManager
    public static APIManager getInstance() {
        if (sInstance == null) {
            synchronized (APIManager.class) {
                if (sInstance == null) {
                    sInstance = new APIManager();
                }
            }
        }
        return sInstance;
    }

    /**
     * 获取对应的Service
     *
     * @param service Service 的 class
     * @param <T>
     * @return
     */
    public <T> T create(Class<T> service) {
        return retrofit.create(service);
    }


    /**
     * 设置订阅 和 所在的线程环境
     */
    public void toSubscribe(Observable observable, Observer observer) {
        observable.subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
//                .retry(RETRY_COUNT)//请求失败重连次数
                .subscribe(observer);

    }

    /**
     * 设置订阅 和 所在的线程环境
     */
    public void toSubscribe(Observable observable, DisposableObserver observer) {
        observable.subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
//                .retry(RETRY_COUNT)//请求失败重连次数
                .subscribe(observer);
    }

}
